import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    ElementRef,
    OnInit
} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Http } from '@angular/http';
import { NotifyService } from '@abp/notify/notify.service';
import { AppComponentBase } from '@shared/common/app-component-base';
import { FileDownloadService } from '@shared/utils/file-download.service';
import {
    ComboboxItemDto,
    MainTaskServiceProxy,
    ReceiveTaskInput
} from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import * as moment from 'moment';
import { finalize } from 'rxjs/operators';
import { PutIntoProductionModalComponent } from '@app/plan/maintasks/put-into-production-modal.component';
import { MenuItem } from 'primeng/api';
@Component({
    templateUrl: './maintask.component.html',
    styleUrls: ['./maintask.component.less'],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class MaintaskComponent extends AppComponentBase implements OnInit {
    // @ViewChild('boundingTeamGroupModal') boundingTeamGroupModal: BoundingTeamGroupModalComponent;
    @ViewChild('putIntoProductionModal')
    putIntoProductionModal: PutIntoProductionModalComponent;
    // @ViewChild('createOrEditOrderModal') createOrEditOrderModal: CreateOrEditOrderModalComponent;

    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;
    //@ViewChild('orderCategoryCombobox') orderCategoryCombobox: ElementRef;
    @ViewChild('statusCombobox') statusCombobox: ElementRef;
    @ViewChild('teamGroupCombobox') teamGroupCombobox: ElementRef;

    advancedFiltersAreShown = false;
    loading: boolean;
    orderCategorys: ComboboxItemDto[];
    statuses: ComboboxItemDto[];
    teamGroups: ComboboxItemDto[];
    isReceived = true;
    isCompleted = false;
    anyncing = false;
    //Filters
    filterText = '';
    organizationUnitId: number = undefined;
    dueDateBegin: moment.Moment = undefined;
    dueDateEnd: moment.Moment = undefined;
    tabs: MenuItem[];
    priority: number = undefined;

    constructor(
        injector: Injector,
        private _mainTaskServiceProxy: MainTaskServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
        this.getTabs();
        this._mainTaskServiceProxy
            .getComboxItemsForSearch()
            .subscribe(result => {
                this.statuses = result.statuses;
                this.teamGroups = result.teamGroups;
            });
        this.loading = true;
    }
    getModels(event?: LazyLoadEvent) {
        this.loading = true;
        this.primengTableHelper.showLoadingIndicator();
        this._mainTaskServiceProxy
            .getMainTasks(
                this.filterText,
                this.isReceived,
                undefined,
                this.isCompleted,
                51,
                this.primengTableHelper.getSkipCount(this.paginator, event),
                this.primengTableHelper.getMaxResultCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
                setTimeout(() => {
                    this.loading = false;
                }, 1000);
            });
    }
    receiveTask(id: number) {
        this.message.confirm('确认接收该条任务吗？', isConfirmed => {
            if (isConfirmed) {
                let rt = new ReceiveTaskInput();
                rt.id = id;
                rt.recipientUserId = 2;
                this._mainTaskServiceProxy.receiveTask(rt).subscribe(() => {
                    this.notify.success('接收成功');
                    this.getModels();
                });
            }
        });
    }
    deleteModel(id: number) {
        this.message.confirm('确认删除该条型号计划吗？', isConfirmed => {
            if (isConfirmed) {
                this._mainTaskServiceProxy.deleteMainTask(id).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    this.getModels();
                });
            }
        });
    }
    getTabs() {
        this.tabs = [
            { label: '已接收', icon: 'fa fa-fw fa-bar-chart' },
            { label: '未接收', icon: 'fa fa-fw fa-calendar' }
            // {label: '已完成', icon: 'fa fa-fw fa-calendar'}
        ];
    }
    tabTypeClick(tab: any) {
        this.isCompleted = false;
        this.isReceived = true;
        if (tab == '已接收') {
            this.isReceived = true;
        } else if (tab == '未接收') {
            this.isReceived = false;
        }
        // else if(tab=="已完成")
        // {
        //   this.isCompleted=true;
        // }
        this.getModels();
    }
}
